/** @format */

import { isAuth } from '@/utils/token'
import { Navigate, Outlet, useLocation } from 'react-router-dom'

/** @format */
type Props = {
  children?: JSX.Element | undefined
}
function AuthRoute({ children }: Props) {
  const location = useLocation()
  const isLogin = isAuth()
  // 未登录
  if (!isLogin) {
    return (
      <Navigate
        to='/login'
        state={{
          redirect: location.pathname
        }}
        replace
      />
    )
  }

  // 否则放行
  return children || <Outlet />
}

export default AuthRoute
